<?php
/**
 * created by zzy
 * date: 2017/10/24 9:28
 */

namespace App\Http\Admin\Process\Controllers;

use App\Engine\Func;
use App\Http\Admin\System\Models\Copy;
use Framework\BaseClass\Http\Admin\Controller;
use App\Eloquent\Ygt\Permission;
use App\Eloquent\Ygt\PrivilegePermission;
use App\Eloquent\Ygt\Process;
use App\Eloquent\Ygt\ProcessFieldRelation;

class IndexController extends Controller
{
    public function index()
    {
    }
    public function lists()
    {
        $admin_user         = request()->user('admin')->toArray();
        $company_id         = $admin_user['company_id'];

        if (request()->isMethod('post')) {
            $type           = request('type','');
            $title          = request('title','');
            $where          = ['company_id'=>$company_id];
            if($type){
                $where['type']      = $type;
            }
            if($title){
                $where['title']     = ['like','%'.$title.'%'];
            }
            $lists          = Process::getList( $where );
            return $lists->toJson();
        } else {
            $typeList       = config('process.process_type_list');
            $typeListJson   = Func::getIdTitleByType($typeList,4);
            return $this->view('lists', compact('typeList','typeListJson') );
        }
    }

    public function edit()
    {
        $admin_user         = request()->user('admin')->toArray();
        $company_id         = $admin_user['company_id'];

        $params             = $this->requestJson();
        $id                 = isset($params['id']) ? $params['id'] : 0;
        $type               = $params['type'];
        $title              = $params['title'];
        $measurementUnit    = $params['measurement_unit'];
        $isSum              = $params['is_sum'];
        $isHideMoneydata    = $params['is_hide_moneydata'];
        $isSameUnit    = $params['is_same_unit'];
        $data               = [
            'type'=>$type,
            'title'=>$title,
            'measurement_unit'=>$measurementUnit,
            'is_sum'=>$isSum,
            'is_hide_moneydata'=>$isHideMoneydata,
            'is_same_unit'=>$isSameUnit,
        ];
        if( $id ){
            $where          = ['company_id'=>$company_id,'id'=>$id];
            $result         = Process::updateOneData( $where, $data );
        } else {
            $data['company_id']= $company_id;
            $result         = Process::insertOneData( $data, 'id' );
        }
        if( $result ){
            return $this->ajaxSuccess( '成功' );
        } else {
            return $this->ajaxFail( '失败' );
        }
    }
    public function delete()
    {
        $admin_user         = request()->user('admin')->toArray();
        $company_id         = $admin_user['company_id'];

        $id                 = request('id',0);
        if( $id && $company_id){
            $id_arr         = explode(',',$id);
            $where          = ['company_id'=>$company_id,'id'=>['in',$id_arr]];
            $del_result     = Process::del($where);
            if( $del_result !== false ){
                $where          = ['process_id'=>['in',$id_arr]];
                $del_result     = ProcessFieldRelation::del($where);
                if( $del_result !== false ){
                    $where      = ['type'=>1, 'type_id'=>['in',$id_arr]];
                    $del_result = Permission::del($where);
                    if( $del_result !== false ){
                        $where      = ['permission_id'=>['in',$id_arr]];
                        $del_result = PrivilegePermission::del($where);
                        if( $del_result !== false ){
                            return $this->ajaxSuccess('成功');
                        } else {
                            return $this->ajaxFail('失败PrivilegePermission');
                        }
                    } else {
                        return $this->ajaxFail('失败Permission');
                    }
                } else {
                    return $this->ajaxFail('失败ProcessFieldRelation');
                }
            }
            return $this->ajaxFail('失败2');
        } else {
            return $this->ajaxFail('失败3');
        }
    }
    public function copy()
    {
        $adminUser          = request()->user('admin')->toArray();
        $companyId          = $adminUser['company_id'];
        $isSubmit           = request('is_submit',0);
        if($isSubmit){
            //return $this->ajaxSuccess( '成功' );exit;
            $params             = request()->all();
            $oldId              = $params['id'];
            $type               = $params['type'];
            $title              = $params['title'];
            $isSum              = $params['is_sum'];
            $isLast             = $params['is_last'];
            $data               = [
                'company_id'=>$companyId,'type'=>$type,
                'title'=>$title,'is_sum'=>$isSum,'is_last'=>$isLast
            ];
            $newId              = Process::insertOneData( $data, 'id' );
            Copy::processFieldRelation($oldId,$newId);
            return $this->ajaxSuccess( '成功' );
        }else{
            $id                 = request('id',0);
            $where              = ['id'=>$id];
            $process            = Process::getInfo($where);
            if($process)
            {
                $info           = $process->toArray();
            }
            $typeList           = config('process.process_type_list');
            return $this->view('copy',compact('info','typeList') );
        }
    }
}